package com.tools.app.ui.view;

import android.animation.Animator;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.PixelFormat;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.airbnb.lottie.LottieAnimationView;
import com.hm.app.R;
import com.hm.base.android.ui.util.ScreenUtil;
import com.tools.cleanmaster.ui.utils.LottieAnimationUtil;

/**
 * Created by ChenLi on 2018/2/26.
 */

public class WeatherGuideWindow {

    protected Context mCtx;
    protected WindowManager mWindowManager;
    private View showView;
    private int showWidth;
    private int showHeight;

    public WeatherGuideWindow(Context ctx) {
        this.mCtx = ctx;
        showWidth = ScreenUtil.dp2px(ctx, 96);
        showHeight = (int) (ScreenUtil.dp2px(ctx, 96));
    }

    public void show() {
        mWindowManager = (WindowManager) mCtx.getSystemService(Context.WINDOW_SERVICE);
        WindowManager.LayoutParams windowViewParams = new WindowManager.LayoutParams();

        //判断权限，选择不同的弹窗方式
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            windowViewParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            windowViewParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
        }

        windowViewParams.format = PixelFormat.RGBA_8888;
        windowViewParams.width = showWidth;
        windowViewParams.height = showHeight;

        showView = LayoutInflater.from(mCtx).inflate(R.layout.weather_guide_layout, null);
        LinearLayout container = showView.findViewById(R.id.lottie_container);
        windowViewParams.x = -ScreenUtil.getScreenWidth(mCtx) / 2;
        windowViewParams.y = ScreenUtil.getScreenHeight(mCtx) - showHeight;
        windowViewParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        mWindowManager.addView(showView, windowViewParams);

        LottieAnimationView aniView = new LottieAnimationView(mCtx);
        LottieAnimationUtil.loadLottieAnimation(mCtx, aniView, "weather", true);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(showWidth, showHeight);
        container.addView(aniView, params);
        aniView.setRepeatCount(1);
        aniView.addAnimatorListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                close();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    }

    /**
     * 判断是否有系统提示框权限
     *
     * @param context
     * @return
     */
    private static boolean checkAlertWindowPermisson(Context context) {
        String packname = context.getPackageName();
        return (PackageManager.PERMISSION_GRANTED == context.getPackageManager().checkPermission("android.permission.SYSTEM_ALERT_WINDOW", packname));
    }

    public void close() {
        try {
            if (mWindowManager != null && showView != null) {
                mWindowManager.removeView(showView);
                showView = null;

            }
        } catch (Exception e) {

        }
    }
}
